package com.search.analysis.impl;

import com.search.FxApplication;
import com.search.analysis.Analysis;
import com.search.entity.SearchContentEntity;
import com.search.utils.ChromeDriverUtil;
import com.search.utils.MapAndBean;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

//@Component
public class Baidu2Analysis implements Analysis { //百度不支持自动化

    @Override
    public String getData(String keyWord) {
        String url = "http://www.baidu.com/s?ie=UTF-8&wd=" + keyWord + "&rn=20&pn=0&lm=360&tn=baidulocal";
        ChromeDriverUtil build = ChromeDriverUtil.build(FxApplication.chromedriver);
        ChromeDriver driver = build.getDriver();
        driver.navigate().to(url);
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
        Boolean until = wait.until(webDriver -> {
            WebElement element = webDriver.findElement(By.cssSelector(".c-container .content-right_8Zs40"));
            return element.isDisplayed();
        });
        if (!until) {
            System.out.println("没有找到元素");
            build.stop();
            return null;
        }
        WebElement element4 = driver.findElement(By.xpath("//*"));
        String source_code = element4.getAttribute("outerHTML");
        build.stop();
        return source_code;
    }

    @Override
    public List<Map<String, Object>> extract(String data) {
        List<Map<String, Object>> list = new CopyOnWriteArrayList<>();
        Document parse = Jsoup.parse(data);
        Elements select = parse.select(".c-container");
        select.stream().parallel().forEach(element -> {
            try {
                Map<String, Object> map = new HashMap<>();
                Element title = element.selectFirst(".c-title a");
                map.put("title", title.text());
                map.put("url", title.attr("href"));
                Element content = element.selectFirst(".content-right_8Zs40");
                if (content != null) {
                    map.put("content", content.text());
                    Element author = element.selectFirst(".c-color-gray");
                    map.put("author", author.text());
                    map.put("source", "百度");
                    map.put("time", "");
                    list.add(map);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        System.out.println("百度查询到的数量:" + list.size());
        return list;
    }

    @Override
    public List<SearchContentEntity> rinse(List<Map<String, Object>> list) {
        List<SearchContentEntity> collect = list.stream().parallel().map(map -> {
            if (map.get("url") == null || map.get("title") == null) {
                return null;
            }
            SearchContentEntity searchContentEntity = MapAndBean.mapToDomain(map, SearchContentEntity.class);
            return searchContentEntity;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SearchContentEntity> process(List<SearchContentEntity> list) {
        return list;
    }


}
